<template>
  <view class="containers">
    <view class="content">
      <view class="edit-bg">
        <view class="edit-bg-text">欢迎使用</view>
        <image
          class="edit-bg-img"
          src="/static/images/edit-bg-icon.png"
        ></image>
      </view>
      <view class="edit-input-box">
        <input
          @input="getInputValue"
          class="input edit-input"
          type="text"
          :value="inputTxt"
          placeholder="请输入充电桩号"
        />
      </view>

      <view class="btn-area">
        <button class="bc-btn_van" @click="keep">确 定</button>
        <button class="cz_btn_van" @click="reset">重 置</button>
      </view>
      <!-- 回到首页 -->
      <view class="back-home" @tap="toIndex">
        <image class="back-icon" src="/static/images/back-home.png"></image>
        <text class="back-name">回到首页</text>
      </view>
    </view>
  </view>
</template>

<script>
const app = getApp();
let Store = app.globalData.Store;
let Sign = app.globalData.Sign;
let Api = app.globalData.Api;
export default {
  components: {
  },
  data() {
    return {
      inputTxt: '',
      deviceCode: '',
    };
  },
  onLoad: function () { },
  onShow: function () { },
  onReady: function () {
  },
  methods: {
    //1 输入终端号
    getInputValue(e) {
      // this.setData({
      this.deviceCode = e.detail.value,
        this.inputTxt = e.detail.value
      // });
    },

    //2 重置输入
    reset() {
      // this.setData({
      this.inputTxt = '',
        this.deviceCode = ''
      // });
    },

    //3 回到首页
    toIndex: function () {
      uni.switchTab({
        url: '/pages/index/index'
      });
    },

    //用户输入 点击确定
    keep() {
      //1 获取终端号
      var device_code = this.deviceCode;
      if (!device_code || device_code == '') {
        return uni.showToast({
          title: '请输入充电桩号',
          icon: 'none',
          duration: 2000
        });
      }
      //2 补全终端号 10091001_1 --> 10091001_01
      if (device_code.indexOf('_') > 0) {
        var codeAr = device_code.split('_');
        var preCode = codeAr[0];
        var sub = parseInt(codeAr[1]);
        device_code = preCode + (sub < 10 ? '_0' + sub : '_' + sub);
      }
      Store.setItem('device_code', device_code);
      // this.setData({
      this.deviceCode = device_code
      // });

      /**
       * 3 区分两种设备的三种协议 分别跳转到选择金额页
       */
      if (device_code.indexOf('_') > 0 && device_code.slice(0, 2) == 10) {
        //电瓶车
        this.dpcMachine(device_code);
      } else {
        //充电桩
        this.cdzMachine(device_code);
      }
    },

    cdzMachine(device_code) {
      const signObject = {
        device_code
      };
      const signvalue = Sign.signutil(signObject);
      app.globalData
        .get(Api.getMachineInfo, {
          device_code,
          sign: signvalue
        })
        .then((res) => {
          if (res.machine != null) {
            if (device_code.indexOf('_') > 0) {
              uni.navigateTo({
                url: '../tidNEWSelMoney/tidNEWSelMoney?device_code=' + device_code + '&bid=10103'
              });
            } else {
              uni.navigateTo({
                url: '../tidOLDSelMoney/tidOLDSelMoney?device_code=' + device_code + '&bid=10103'
              });
            }
          } else {
            uni.showModal({
              title: '提示',
              content: '无充电桩信息',
              success(res) {
                if (res.confirm) {
                } else if (res.cancel) {
                }
              }
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },

    dpcMachine(device_code) {
      const signObject = {
        device_code
      };
      const signvalue = Sign.signutil(signObject);
      //查询电瓶车
      app.globalData
        .get(Api.getDPCMachineInfo, {
          device_code,
          sign: signvalue
        })
        .then((res) => {
          if (res.machine != null) {
            uni.navigateTo({
              url: '../tidDPCSelMoney/tidDPCSelMoney?device_code=' + device_code
            });
          } else {
            uni.showModal({
              title: '提示',
              content: '无充电位信息',
              success(res) {
                if (res.confirm) {
                } else if (res.cancel) {
                }
              }
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    }
  }
};
</script>
<style>
@import "./sysInput.css";
</style>
